﻿@{
    Layout = "~/Views/Shared/_List.cshtml";
    var module = "/Sys/Module";
    var moduleName = "模块";
    var moduleSize = new { width = 500, height = 500 };
}
<div class="layui-search">
    <a id="btnAdd" class="layui-btn">新增</a><a id="btnDelist" class="layui-btn" data-type="delist">删除</a>
    <div class="layui-form-search layui-form">
        <div class="layui-inline">
            <input id="txtKeywords" class="layui-input" placeholder="请输入查询条件" data-type="reload">
        </div>
        <div class="layui-inline">
            <select id="sel_Status">
                <option value="-1">全部</option>
                @foreach (Common.Utility.SelectItem item in ViewBag.Status)
                {
                    <option value="@(item.Value)">@(item.Text)</option>
                }
            </select>
        </div>
        <a id="btnSearch" class="layui-btn" data-type="reload">查询</a>
    </div>
</div>
<table id="list" lay-filter="list"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
@section script{
    <script>
        var $;
        layui.use(['table'], function() {
            var table = layui.table;
            var tableIns = table.render({
                elem: '#list'
                , id: 'list'
                , url: '@module/List'
                , page: true
                , limits: [10, 15, 20, 25, 30]
                , limit: 15
                , height: 'full-68'
                , cols: [[ //标题栏
                    { checkbox: true, fixed: true }
                    , { title: '模块名称', field: 'name', width: 200 }
                    , { title: '模块类型', field: 'typeName', width: 120 }
                    , { title: '访问地址', field: 'pageUrl', width: 300 }
                    , { title: '状态', field: 'statusName', width: 120 }
                    , { title: '创建时间', field: 'createTime', width: 200 }
                    , { title: '操作', width: 160, align: 'center', toolbar: '#barDemo', fixed: "right" }
                ]]
            });

            $ = layui.$;
            var active = {
                reload: function() {

                    table.reload('list', {
                        where: {
                            Keywords: $('#txtKeywords').val(),
                            Status: $('#sel_Status').val()
                        }, page: {
                            curr: 1
                        }
                    });
                },
                delist: function() { //获取选中数据
                    var checkStatus = table.checkStatus('list')
                        , data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg('未选中任何数据', { icon: 0 });
                        return;
                    }
                    var ids = [];
                    $.each(data, function(index, item) {
                        ids.push(item.id);
                    })
                    layer.confirm('真的删除所选行么', function(index) {
                        $.post("@module/Delist", { ids: ids }, function(d) {
                            if (d.code === 0) {
                                layer.msg(d.msg, { icon: 1 });
                                $('#btnSearch').click();
                            }
                            else {
                                layer.msg(d.msg, { icon: 2 });
                            }
                        });
                        layer.close(index);
                    });
                }
            };

            function activeCall(obj) {
                var type = $(obj).data('type');
                active[type] ? active[type].call(obj) : '';
            }
            $('#btnSearch,#btnDelist').on('click', function() {
                activeCall(this);
            });
            $('#txtKeyword').on('keyup', function(e) {
                if (e.keyCode === 13) activeCall(this);
            });
            $('#btnAdd').click(function() {
                add('新增@(moduleName)', '@module/Edit', '@moduleSize.width', '@moduleSize.height', false, function () { table.reloadData('list', {}); });
            })

            //监听工具条
            table.on('tool(list)', function(obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function(index) {
                        $.post("@module/Del", { id: data.id }, function(d) {
                            if (d.code === 0) {
                                layer.msg(d.msg, { icon: 1 });
                                $('#btnSearch').click();
                            }
                            else {
                                layer.msg(d.msg, { icon: 2 });
                            }
                        });
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    add('编辑@(moduleName)', '@module/Edit?id=' + data.id, '@moduleSize.width', '@moduleSize.height', false, function () { table.reloadData('list', {}); });
                }
            });
        });
    </script>
}